package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Cust;
import com.gk.carshopsys.pojo.Emp;
import com.gk.carshopsys.service.CustService;
import com.gk.carshopsys.utils.CustDataListener;
import com.gk.carshopsys.utils.EmpDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CustController {
    @Autowired
    CustService custService;



    //Excel参数
    private HashMap<String,String> param;


    //excel传参
    @PostMapping("/custlist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }


    //excel下载
    @Log("导出客户")
    @GetMapping("/custlist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
        //导出的参数

        String custid_s = param.get("custid");
        String custname = param.get("custname");
        String job = param.get("job");
        String address = param.get("address");

        Cust cust = new Cust();

        if (custid_s!=null && !custid_s.equals("")){
            try{
                Long custid=Long.parseLong(custid_s);
                cust.setid(custid);
            }catch (ClassCastException e){

            }finally {
                cust.setCust_name(custname);
                cust.setJob(job);
                cust.setAddress(address);
                ExcelUtils.export2Web(response, "客户表", "客户信息",Cust.class,custService.findCust(cust));
                return "success";

            }
        }else {
            cust.setCust_name(custname);
            cust.setJob(job);
            cust.setAddress(address);
            ExcelUtils.export2Web(response, "客户表", "客户信息",Cust.class,custService.findCust(cust));
            return "success";
        }



    }

    // easyexcel上传文件
    @Log("导入客户")
    @PostMapping("/custlist/excelimport")
    public String upload(MultipartFile file, RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), Cust.class, new CustDataListener(custService)).sheet().doRead();
        return "redirect:/custlist";
    }

    //跳转客户管理页面
    @RequestMapping("/custlist")
    public String tocustlist(){
        return "custlist";
    }

    //返回查询客户列表
    @Log("查询客户")
    @RequestMapping("/custlist/query")
    @ResponseBody
    public List<Cust> findcust(@RequestBody HashMap<String,String> params){
        //接收的数据
        String custid_s = params.get("custid");
        String custname = params.get("custname");
        String job = params.get("job");
        String address = params.get("address");



        Cust cust = new Cust();

        if (custid_s!=null && !custid_s.equals("")){
            try{
                Long custid=Long.parseLong(custid_s);
                cust.setid(custid);
            }catch (ClassCastException e){

            }finally {
                cust.setCust_name(custname);
                cust.setJob(job);
                cust.setAddress(address);

                return custService.findCust(cust);

            }
        }else {
            cust.setCust_name(custname);
            cust.setJob(job);
            cust.setAddress(address);

            return custService.findCust(cust);
        }



    }





//    添加客户
    @Log("添加客户")
    @PostMapping("/custlist/add")
    public  String addcust(@RequestParam("add_custname") String name,
                           @RequestParam("add_sex") String sex,
                           @RequestParam("add_job") String job,
                           @RequestParam("add_telphone") String telphone,
                           @RequestParam("add_address") String address,
                           RedirectAttributesModelMap model){
        Cust add_cust =new Cust(name,sex,job,telphone,address);
        int result = custService.addCust(add_cust);

        if(result==1){
            model.addFlashAttribute("addmsg","添加客户成功");
            return "redirect:/custlist";
        }else {
            model.addFlashAttribute("addmsg","添加客户失败，未知错误");
            return "redirect:/custlist";
        }

   }


   //更新修改客户
    @Log("更新客户")
    @PostMapping("/custlist/update")
    public String updatecust(@RequestParam("update_custid") Long id,
                             @RequestParam("update_custname") String name,
                             @RequestParam("update_sex") String sex,
                             @RequestParam("update_job") String job,
                             @RequestParam("update_telphone") String telphone,
                             @RequestParam("update_address") String address,
                             RedirectAttributesModelMap model){

        Cust update_cust =new Cust(id,name,sex,job,telphone,address);
        int result = custService.updateCust(update_cust);

        if(result==1){
            model.addFlashAttribute("updatemsg","修改客户成功");
            return "redirect:/custlist";
        }else {
            model.addFlashAttribute("updatemsg","修改客户失败，未知错误");
            return "redirect:/custlist";
        }

    }

    //批量删除客户
    //客户批量删除
    @Log("删除客户")
    @ResponseBody
    @RequestMapping("/custlist/del")
    public Map<String,Integer> custlistdel(@RequestBody List<Integer> ids){
        int result = custService.delCust(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }

}
